/**
	装载下拉框的角色列表
 */
var searchForm = $(document.forms.searchForm);
var mallTypeId = $(document.forms.searchForm.mallTypeId);
$.get(
	"admin/product.let?action=malltpye",
	function(data){
		for(let mallType of data) {
			mallTypeId.append(`<option value="${mallType.mallTypeId}">${mallType.mallTypeName}</option>`);
		}
	},"json"
);

//保存查询条件
var searchParams = new URLSearchParams();
searchForm.on("submit",function(){
	searchParams = sys.form.param(searchForm[0]);
	datagrid.bootstrapTable("refresh",{pageNumber:1});
	return false;
});


var datagrid = $("#datagrid").bootstrapTable({
	url: "admin/product.let?action=page",
	dataField: "list",//rows
	totalField: "total",
	queryParamsType: "",//limit
	pagination: true,
	sidePagination: "server",//client
	queryParams:function(params) { 
	  for(let name of searchParams.keys()){//添加搜索条件
		  console.info(name+"="+searchParams.get(name));
		  params[name]=searchParams.get(name);
	  }
	  console.info(params);
	  return params 
    }
});



var indexFormatter = function(value, row, index, fieldName) {
	return index + 1;
}


var imgsFormatter = function(fieldName) {
	return `<img src="${fieldName}" width= "150px" height="150px" onerror="this.src='assets/img/no-img.png'">`;
}

/**
 * 格式化表格操作菜单
 */
var optionFormatter = function(value, row, index) {
	return `<div class="dropdown">
  <button class="btn btn-outline-primary btn-block dropdown-toggle" type="button" data-toggle="dropdown">
    操作
  </button>
  <div class="dropdown-menu dropdown-menu-right">
    <a class="dropdown-item "href="admin/product.let?action=findProduct&productId=${value}">查看商品信息</a>
     <a class="dropdown-item "href="admin/product.let?action=updateProductForm&productId=${value}">修改商品信息</a>
    <a class="dropdown-item _delete" data-index="${index}" href="javascript:void(0);">下架该商品</a>
  </div>
</div>`;
}


datagrid.on("click", "._delete", function() {//删除用户
	let obj = $(this);
	let index = obj.data("index");//data-index
	let row = datagrid.bootstrapTable("getData")[index];
	sys.confirm(`您确定要下架[${row.productId}]号商品吗？`, function(r) {
		if (r) {
			$.post(
				"admin/product.let?action=delete",
				{ "productId": row.productId },
				function(data) {
					if (data.code == 200) {
						sys.toastr.success(`商品[${row.productId}]下架成功`);
						datagrid.bootstrapTable("refresh");
					} else {
						sys.toastr.error(data.message);
					}
				}, "json"
			);
		}
	});
});
